|
1
|
|
|
import rimraf from "rimraf"; |
|
2
|
|
|
import * as fs from "fs"; |
|
3
|
|
|
import * as path from "path"; |
|
4
|
|
|
import log from "./log"; |
|
5
|
|
|
//import { core } from "./core"; |
|
6
|
|
|
import core from "./core"; |
|
7
|
|
|
|
|
8
|
|
|
class filemanager { |
|
9
|
|
|
/** |
|
10
|
|
|
* Delete file or directory recursive |
|
11
|
|
|
* @param filedir |
|
12
|
|
|
* @param async |
|
13
|
|
|
* @returns null = file/dir not exists, false = delete filedir failed, true = success |
|
14
|
|
|
*/ |
|
15
|
|
|
static unlink(filedir: string, async?: boolean) { |
|
16
|
|
|
const deleteNow = function () { |
|
17
|
|
|
if (fs.existsSync(filedir)) { |
|
18
|
|
|
if (async) { |
|
19
|
|
|
rimraf(filedir, function (err) { |
|
20
|
|
|
if (!err) { |
|
21
|
|
|
log.log(log.success("done")); |
|
22
|
|
|
} else { |
|
23
|
|
|
log.log(log.error(`cannot delete ${core.filelog(filedir)}`)); |
|
24
|
|
|
} |
|
25
|
|
|
}); |
|
26
|
|
|
} else { |
|
27
|
|
|
rimraf.sync(filedir); |
|
28
|
|
|
} |
|
29
|
|
|
return true; |
|
30
|
|
|
} else { |
|
31
|
|
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
}; |
|
34
|
|
|
try { |
|
35
|
|
|
var exists = fs.existsSync(filedir); |
|
36
|
|
|
if (exists) { |
|
37
|
|
|
return deleteNow(); |
|
38
|
|
|
} else { |
|
39
|
|
|
return null; |
|
40
|
|
|
} |
|
41
|
|
|
} catch (error) { |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* create file recursive |
|
48
|
|
|
* @param file |
|
49
|
|
|
* @param content |
|
50
|
|
|
*/ |
|
51
|
|
|
static mkfile(file: string, content: any) { |
|
52
|
|
|
this.mkdir(path.dirname(file)); |
|
53
|
|
|
if (typeof content == "object" || Array.isArray(content)) { |
|
54
|
|
|
content = JSON.stringify(content, null, 4); |
|
55
|
|
|
} |
|
56
|
|
|
fs.writeFileSync(file, content, { encoding: "utf-8" }); |
|
57
|
|
|
return file; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* create directory recursive |
|
62
|
|
|
* @param dir |
|
63
|
|
|
*/ |
|
64
|
|
|
static mkdir(dir: string) { |
|
65
|
|
|
if (!fs.existsSync(path.dirname(dir))) { |
|
66
|
|
|
this.mkdir(path.dirname(dir)); |
|
67
|
|
|
} |
|
68
|
|
|
if (!fs.existsSync(dir)) { |
|
69
|
|
|
fs.mkdirSync(dir); |
|
70
|
|
|
} |
|
71
|
|
|
return dir; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* remove all files/folders except matches regex |
|
76
|
|
|
* @param folder |
|
77
|
|
|
* @param exclude |
|
78
|
|
|
*/ |
|
79
|
|
|
static empty(folder: string, exclude: RegExp | null) { |
|
80
|
|
|
fs.readdir(folder, (err, files) => { |
|
81
|
|
|
if (err) { |
|
82
|
|
|
log.log(log.error(err.message)); |
|
83
|
|
|
} else { |
|
84
|
|
|
files.forEach((file) => { |
|
85
|
|
|
const fileDir = path.join(folder, file); |
|
86
|
|
|
|
|
87
|
|
|
if (exclude) { |
|
88
|
|
|
if (!exclude.test(file)) { |
|
89
|
|
|
filemanager.unlink(fileDir, true); |
|
90
|
|
|
} else { |
|
91
|
|
|
log.log(log.error(`${fileDir} in excluded lists`)); |
|
92
|
|
|
} |
|
93
|
|
|
} else { |
|
94
|
|
|
filemanager.unlink(fileDir, true); |
|
95
|
|
|
} |
|
96
|
|
|
}); |
|
97
|
|
|
} |
|
98
|
|
|
}); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
export = filemanager; |
|
103
|
|
|
|